package sg.edu.nus.iss.jass.gui;

import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * <h5>FormPanel to load the actual GUI form </h5>
 * This  abstract class is derived from JPanel to create the GUI forms using AWT components.
 * All the derived classes from this FormPanel class must create form according to the use 
 * case screen to build the form by using Swing/AWT components in the show() method.
 * @author Kafoor Sammil
 * 
 */
public  abstract class FormPanel extends JPanel implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8277505030025908721L;
	
	/**
     * The actionPanel which will have action buttons .
     */ 
	protected ActionPanel actionPanel;
	/**
     * The headerPanel which will have header panel for displaying both Title and Messages on the form .
     */ 
	protected HeaderPanel headerPanel;
	
	/**
     * The reference to the container frame which loaded this panel.
     */ 
	private MainFrame mainframe = null;
	
	/**
	 * Constructor to create the FormPanel Object .
	 * @param mainframe
	 *            reference to the container mainframe object. 
	 * @param actionPanel
	 *            reference to the actionPanel object which displays the action buttons.
	 * @param headerPanel
	 *            reference to the headerPanel object which displays Title and Message.    
	 */
	public FormPanel(MainFrame mainframe,ActionPanel actionPanel, HeaderPanel headerPanel){
		super(null);
		this.mainframe = mainframe;
		this.headerPanel = headerPanel;
		this.actionPanel = actionPanel;
    	this.actionPanel.addActionListener(this);
	}
	
	/**
	 *  Method to add the current FormPanel to the mainframe. This will be called by all 
	 *  the classes derived from FormPanel class
	 */
	protected void addFormPanel(){
		mainframe.addFormPanel(this);
	}
	
	/**
	 *  Method to show the Confirmation Dialog .This is to get the 
	 *  confirmation from the user before doing any operations.
	 * @param function
	 *            string use case function (e.g Job Advert/ JobRegister). 
	 * @param action
	 *            string action (e.g add/delete/update/withdraw).
	 */
	public int showConfirmDialog(String function, String action){
		
		int opt = JOptionPane.showConfirmDialog(null, 
				"Do you  want to "+ action+ " " + function + "?",
				"Confirm Action",
				JOptionPane.YES_NO_OPTION);
		
		return opt;
	}
	
	/**
	 *  Method to show the resultant (Success/Error) action completion message on the Header Panel.
	 * @param msgText
	 *            string use case function (e.g Job Advert/ JobRegister). 
	 * @param success
	 *            true -  show the message in green color for success, red for otherwise .
	 */
    public void showMsg(String msgText,boolean success){
		if(this.headerPanel != null){
    		this.headerPanel.setMsgText( msgText, success);
    	}
	}
    
    /**
	 *  Method to clear resultant (Success/Error) action completion message on the Header Panel.  
	 *  */
    public void clearMsg() {
    	if(this.headerPanel != null){
    		this.headerPanel.clearMsg();
    	}
		
	}
    
    /**
	 *  Abstract Method to create form  to build the form by using Swing/AWT components.All the derived 
	 *  classes from FormPanel must implement this method to construct the actual form. 
	 *  */
     public abstract  void show();

}
